package studio.xiaoyun.web;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import studio.xiaoyun.core.dao.IFeedbackDao;
import studio.xiaoyun.core.entity.Feedback;
import studio.xiaoyun.core.parameter.FeedbackParameter;
import studio.xiaoyun.web.resource.FeedbackResource;

/**
 * 辅助类，用于将实体类转换为资源类。
 * <p>方法的参数中应该有一个是{@link studio.xiaoyun.core.parameter.Parameter 参数类}，
 * {@link studio.xiaoyun.web.interceptor.ResourceUtilInterceptor 拦截器}会拦截每个方法，根据参数类的设置将多余的数据删除
 * @author 岳正灵
 * @see studio.xiaoyun.web.PublicParameter#FIELD FIELD
 */
@Service
public class ResourceUtil {
	@Resource
	private IFeedbackDao feedbackDao;
	
    /**
     * 将Feedback类转换为FeedbackResource类
     * @param feedback Feedback类的实例
     * @param parameter 参数类型
     * @return FeedbackResource类的实例
     */
    public FeedbackResource toFeedbackResource(Feedback feedback,FeedbackParameter parameter){
    	List<FeedbackResource> resources = this.toFeedbackResource(Arrays.asList(feedback),parameter);
    	return resources.get(0);
    }
    
    /**
     * 将Feedback类转换为FeedbackResource类
     * @param feedbacks Feedback类的实例
     * @param parameter 参数类型
     * @return FeedbackResource类的实例
     */
    public List<FeedbackResource> toFeedbackResource(List<Feedback> feedbacks,FeedbackParameter parameter){
    	List<FeedbackResource> resources = new LinkedList<FeedbackResource>();
    	for(Feedback feedback:feedbacks){
    		FeedbackResource resource = new FeedbackResource();
        	resource.setFeedbackID(feedback.getFeedbackID());
        	resource.setText(feedback.getText());
        	resource.setTitle(feedback.getTitle());
        	resource.setCreateDate(feedback.getCreateDate());
        	resources.add(resource);
    	}
    	return resources;
    }
    
	public IFeedbackDao getFeedbackDao() {
		return feedbackDao;
	}
	public void setFeedbackDao(IFeedbackDao feedbackDao) {
		this.feedbackDao = feedbackDao;
	}
}
